<?php

namespace App\Repository\Commit;

use App\Models\DictConfig as model;
use App\Repository\BaseRepository;

class DictConfigRepository extends BaseRepository
{
    public function __construct()
    {
        $this->model = new model;
        parent::__construct();
    }

    public function index($request)
    {
        $cron['order']['orderby'] = isset($request['orderby']) ? $request['orderby'] : 'id';
        $cron['order']['orderdir'] = isset($request['orderdir']) ? $request['orderdir'] : 'desc';
        $cron['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
        $cron['limit']['number'] = $request->get('pageSize');
        $rows = $this->search($this->model, $cron);
//        echo $this->getLastSql();
        return $rows;
    }

    public function add($params)
    {
        return $this->edit($params);
    }

    public function edit($params)
    {
        $params['shop_id'] = $params['user']['shop_id'] ?? 0;
        //必填 字段
        $mustField = [
            'keys',
            'type'
        ];
        $optionalFields = $this->model->getTableField();
        $msgField = [
            'keys' => '字段不能为空',
            'type' => '值类形不能为空'
        ];
        $insertData = $this->getRealData($params, $mustField, $optionalFields, $msgField);
        if (!empty($insertData) && is_string($insertData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $insertData
            );
        }
//        数组值转json
        if ($insertData['type'] == 3) {
            $insertData['value'] = json_encode($insertData['value']);
        }
        if (isset($params['id']) && $params['id'] > 0) {
            unset($insertData['id'], $insertData['keys'], $insertData['shop_id']);
            $res = $this->model->where(['id' => $params['id']])->update($insertData);
            if (false !== $res) {
                return $this->returnMsg(200, '修改成功!');
            }
            return $this->returnMsg(3, '请重新修改');
        } else {
            $isTrue = $this->model->where(['keys' => $insertData['keys'], 'shop_id' => $insertData['shop_id']])->count();
            if ($isTrue) {
                return $this->returnMsg(1, '该字段已经存在');
            }
//            记录创建者
            $insertData['users_id'] = $params['user']['id'] ?? 0;
            $res = $this->model->insert($insertData);
            if ($res) {
                return $this->returnMsg(200, '添加成功');
            }
            return $this->returnMsg(1, '添加失败');
        }
    }

//返回给前端
    public function getDataByKeys($where)
    {
        $rows = $this->model->searchWhere($where)->get();
        $data = [];
        foreach ($rows as $k => $v) {
            unset($v['shop_id'], $v['users_id']);
            $v['value'] = $this->model->makeTypeValue($v['type'], $v['value']);
            $data[$v['keys']] = $v;
        }
        unset($rows);
        return $data;
    }

    public function getValue($key)
    {
        return $this->model->getValue($key);
    }

}
